<?php


namespace app\admin\model;
use      think\Model;

class Admin extends Model
{
   public function addadmin($data){


      //如果数据不为空 就执行添加

       if(empty($data)||!is_array($data)){
           return false;
       }

       if($data['password']){
           $data['password'] = md5($data['password']);
       }
       if($this->save([
           'name'=>$data['name'],
           'password'=>$data['password']
       ])){
           $groupAccess['uid']=$this->id;
           $groupAccess['group_id']=$data['group_id'];
           db('auth_group_access')->insert($groupAccess);
           return true;
       }else{
           return false;
       }
   }
   public function getadmin(){
       //模型动态查询
       return $this::paginate(1);
   }
   public function modifyadmin($data,$post){
       if(empty($post)||!is_array($post)){
           return false;
       }else{
           if(!$post['name']){
               return 2;
           }
           if($post['password']){
               $post['password'] = md5($post['password']);
           }else{
               $post['password'] = $data['password'];
           }
       }
       return $this->force()->save([
           'name'=>$post['name'],
           'password'=>$post['password']
       ],['id'=>$data['id']]);
   }
   public function deladmin($id){
       $admin = $this->get($id);
      return $admin->delete();
   }
   public function login($data){
       $admin = Admin::getByName($data['name']);
       if($admin){
          $data['password'] = md5($data['password']);
          if($data['password']===$admin['password']){
              session('id',$admin['id']);
              session('name',$admin['name']);
              return 2;
          }else{
              return 3;
          }
       }else{
           return 1;//用户名不存在
       }
       die();
   }

}